﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoeObjects;
using DoeLibsMVC.Security;

namespace DoeLibsMVC.Controllers
{
    public abstract class BaseController : Controller
    {
        protected virtual new CustomPrincipal User
        {
            get { return HttpContext.User as CustomPrincipal; }
        }

        /// <summary>
        /// field which contains the message.
        /// </summary>
        public HttpMessage Message
        {
            get
            {
                if (Request.Cookies.AllKeys.Contains(HttpMessage.MESSAGE_COOKIE_NAME) && !string.IsNullOrEmpty(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value))
                {
                    HttpMessage msg = HttpMessage.getFromJson(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value);

                    return msg;
                }
                return null;
            }
            set
            {
                if (value == null)
                {
                    //expire old cookie (-->delete message)
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(1);
                    cookie.Value = value.getJson();
                    Response.Cookies.Add(cookie);
                }
            }
        }

        public BaseController()
        {
            
        }

        protected override void OnActionExecuting(ActionExecutingContext ctx) 
        {
            //add search options:
            List<string> optionList = new List<string>();
            optionList.Add("Title");
            optionList.Add("Author");
            optionList.Add("ISBN-10");
            optionList.Add("ISBN-13");
			optionList.Add("Topic");
            ViewBag.list = new SelectList(optionList);
            
        }

    }
}
